$(function () {
    var fw = $("#wzbm-fw").val();
    var treeUrl = "/sys/wzbm/get_treejson";
    if (fw == 1) {
        treeUrl = "/organ/bmfw/get_treejson";
    }
    $("[name='search-wzbm-tree']").tree({
        url: $.webPath() + treeUrl,
        onClick: function (node) {
            if (node.attributes.level == 3) {
                $(searchGrid).datagrid({
                    url: $.webPath() + "/sys/wzbm/get_gridjson",
                    fitColumns: true,
                    fit: true,
                    pageNumber: 1,
                    queryParams: {"level": node.attributes.level, "wzbmId": node.id.split(":")[0]},
                    columns: [
                        [
                            {title: "名称", field: "name", width: 100},
                            {title: "编码", field: "oldcode", width: 100, hidden: true},
                            {title: "编码(新)", field: "code", width: 100, hidden: true},
                            {title: "规格型号", field: "ggxh", width: 100},
                            {title: "规格简称", field: "ggjc", width: 100, hidden: true},
                            {title: "材质", field: "cz", width: 100},
                            {title: "单位", field: "dw", width: 100},
//                            {title: "是否报价", field: "sfbj", width: 100},
                            {title: "所属品名", field: "pm_name", width: 100}
                        ]
                    ],
                    onSelect: function (rowIndex, rowData) {
//                        var flag=true;
//                        var rows=$(selectedGrid).datagrid("getRows");
//                        $.each(rows,function(i,obj){
//                              if(obj.id==rowData.id){
//                                    flag=false;
//                                   return false;
//                              }
//                        });
//                        if(flag){
                        $(selectedGrid).datagrid('appendRow', rowData);
                        $("[name='deleteSelect']").linkbutton({
                            plain: true,
                            iconCls: 'icon-cancel'
                        });
//                        }
                    }
                });
            }
            else {
                $("[name='search-wzbm-tree']").tree("expand", node.target);
            }
        }
    });

    var searchGrid = $("#search-wzbm-grid").datagrid({
        columns: [
            [
                {title: "名称", field: "name", width: 100},
                {title: "编码", field: "oldcode", width: 100, hidden: true},
                {title: "编码(新)", field: "code", width: 100, hidden: true},
                {title: "规格型号", field: "ggxh", width: 100},
                {title: "规格简称", field: "ggjc", width: 100, hidden: true},
                {title: "材质", field: "cz", width: 100},
                {title: "单位", field: "dw", width: 100},
                // {title: "是否报价", field: "sfbj", width: 100},
                {title: "所属品名", field: "pm_name", width: 100}
            ]
        ]
    });


    var selectedGrid = $("#search-wzbm-selected-wzbm-grid").datagrid({
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        columns: [
            [
                {title: "名称", field: "name", width: 100},
                {title: "编码", field: "oldcode", width: 100, hidden: true},
                {title: "编码(新)", field: "code", width: 100, hidden: true},
                {title: "规格型号", field: "ggxh", width: 100},
                {title: "规格简称", field: "ggjc", width: 100, hidden: true},
                {title: "材质", field: "cz", width: 100},
                {title: "单位", field: "dw", width: 100},
                //{title: "是否报价", field: "sfbj", width: 100},
                {title: "所属品名", field: "pm_name", width: 100},
                {title: "操作", width: 40, align: 'center', field: "id", formatter: function (value, row, index) {
                    return '<a name="deleteSelect" val="' + value + '"></a>';
                }}
            ]
        ],
        onRowContextMenu: function (e, rowIndex, rowData) {
            e.preventDefault();
            $('#search-wzbm-menu').menu('show', {
                left: e.pageX,
                top: e.pageY
            }).data("rowIndex", rowIndex);
        },
        onClickCell: function (rowIndex, field, value) {
            if (field == "id") {
                $(selectedGrid).datagrid('deleteRow', rowIndex);
            }
        }
    });

    $("#search-wzbm-menu").menu({
        onClick: function (item) {
            if (item.name == "delete")
                $("#search-wzbm-selected-wzbm-grid").datagrid('deleteRow', $(this).data("rowIndex"));
        }
    });

    $("#search-wzbm-toolbar :button").click(function () {
        $('#search-wzbm-grid').datagrid({
            url: $.webPath() + "/sys/wzbm/search?fw=" + fw,
            pageNumber: 1,
            queryParams: {
                name: $('#mc').val(),
                code: $('#code').val(),
                cz: $("#cz").val(),
                ggxh: $("#ggxh").val()
            },
            columns: [
                [
                    {title: "名称", field: "name", width: 100},
                    {title: "编码", field: "oldcode", width: 100, hidden: true},
                    {title: "编码(新)", field: "code", width: 100, hidden: true},
                    {title: "规格型号", field: "ggxh", width: 100},
                    {title: "规格简称", field: "ggjc", width: 100, hidden: true},
                    {title: "材质", field: "cz", width: 100},
                    {title: "单位", field: "dw", width: 100},
                    {title: "是否报价", field: "sfbj", width: 100, formatter: function (value, row, index) {
                        return value ? '是' : '否'
                    }},
                    {title: "所属品名", field: "pm_name", width: 100}
                ]
            ],
            onSelect: function (rowIndex, rowData) {
                $(selectedGrid).datagrid('appendRow', rowData);
                $("[name='deleteSelect']").linkbutton({
                    plain: true,
                    iconCls: 'icon-cancel'
                });
            }/*,
             onLoadSuccess:function(){
             var row = $(this).datagrid("getRows").length;
             if(row==1){
             $(this).datagrid('selectRow', 0);
             }
             }*/
        });
    });
})

